﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using DTO;
using BUS;
namespace GUI
{
    public partial class ThemHoaDonGUI : Window
    {
        public ThemHoaDonGUI()
        {
            InitializeComponent();
            hoiThoat = false;
            
        }
        int IsMember(ComboBox cb)
        {
            KhachHangDTO dto = (KhachHangDTO)cbbLoaiKH.SelectedItem;
            if (dto.MaKH == 2)
                return 1;
            return 0;
        }
        void ThanhTienTungSP()
        {
            int sl;
            bool kq = int.TryParse(txtSL.Text, out sl);
            if (kq == true)
            {
                if (this.IsMember(cbbLoaiKH) == 1)
                {
                    sl = sl * int.Parse(txtDonGiaTV.Text);
                    txtThanhTien.Text = sl.ToString();
                }
                else
                {
                    sl = sl * int.Parse(txtDonGiaKL.Text);
                    txtThanhTien.Text = sl.ToString();
                }
            }
            else
                txtSL.Focus();

        }
        private void ThemHoaDonGUI_Load(object sender, EventArgs e)
        {
            BUS.SanPhamBUS sp = new BUS.SanPhamBUS();
            cbbsanpham.DataSource = sp.LayDSSP();
            cbbsanpham.DisplayMember = "TenSP";
            dataGridView1.Columns.Add("c1", "Mã sản phẩm");
            dataGridView1.Columns.Add("c2", "Tên sản phẩm");
            dataGridView1.Columns.Add("c3", "Số lượng");
            dataGridView1.Columns.Add("c4", "Đơn giá");
            dataGridView1.Columns.Add("c5", "Thành tiền");
            dataGridView1.Columns.Add("c6", "Điểm CV");


            BUS.NhanVienBUS nv = new BUS.NhanVienBUS();
            BUS.KhachHangBUS kh = new BUS.KhachHangBUS();
            cbbMaTV.DataSource = nv.LayDSNV();
            cbbMaTV.DisplayMember = "HoTen";
            cbbLoaiKH.DataSource = kh.SelectAll();
            cbbLoaiKH.DisplayMember = "MaKH";
        }
        private void cbbsanpham_SelectedIndexChanged(object sender, EventArgs e)
        {
            SanPhamDTO sp = (SanPhamDTO)cbbsanpham.SelectedItem;
            txtMaSP.Text = sp.MaSP.ToString();
            txtTenSP.Text = sp.TenSP;
            txtDonGiaTV.Text = sp.DonGiaTV.ToString();
            txtDonGiaKL.Text = sp.DonGiaKL.ToString();
            txtQuyCach.Text = sp.QuyCach;
            txtDVT.Text = sp.DVT;
            txtSLTon.Text = sp.SLTon.ToString();
            txtDiemCV.Text = sp.DiemCV.ToString();
            txtSL.Text = "";
            txtThanhTien.Text = "";
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            this.ThanhTienTungSP();
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtSL.Text == "")
            {
                MessageBox.Show("Chưa nhập số lượng");
                txtSL.Focus();
                return;
            }
            else 
            {
                if (!KiemTraSo(txtSL.Text))
                {
                    MessageBox.Show("Số lượng nhập ký tự 0--->9");
                    txtSL.Focus();
                    return;
                }
            }
            DTO.SanPhamDTO sp = (DTO.SanPhamDTO)cbbsanpham.SelectedItem;
            if (ktTonTai(dataGridView1, sp) == 1)
            {
                MessageBox.Show("Sản phẩm đã tồn tại");
                return;
            }
            int sl = int.Parse(txtSL.Text);
            if (sp.SLTon == 0 || sp.SLTon < sl)
            {
                MessageBox.Show("Không còn hàng hoặc không đủ số lượng để bán");
                txtSL.Focus();
                return;
            }
            dataGridView1.Rows.Add(sp.MaSP, sp.TenSP, sl, sp.DonGiaTV, double.Parse(txtThanhTien.Text), sp.DiemCV);
            
           

        }
        private void btnChapNhan_Click(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount==0)
            {
                MessageBox.Show("Chưa thêm sản phẩm cho hóa đơn");
                return;
            }
            BUS.HoaDonBUS bus = new BUS.HoaDonBUS();
            BUS.CTHoaDonBUS ct = new BUS.CTHoaDonBUS();
            DTO.HoaDonDTO hd = new HoaDonDTO();
            bus.ThemHoaDOn(hd);
            int tongcv = 0;
            int NewHoaDon = bus.LayHoaDonMoi();
            CTHoaDonDTO cthd = new CTHoaDonDTO();
            #region Thêm CTHoaDon
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                cthd.MaHD = NewHoaDon;
                cthd.MaSP = int.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
                cthd.SoHD = "0";
                cthd.TenSP = dataGridView1.Rows[i].Cells[1].Value.ToString();
                cthd.SoLuong = int.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString());
                cthd.DonGia = double.Parse(dataGridView1.Rows[i].Cells[3].Value.ToString());
                cthd.ThanhTien = cthd.SoLuong * cthd.DonGia;
                cthd.DiemCV = int.Parse(dataGridView1.Rows[i].Cells[5].Value.ToString()) * cthd.SoLuong;
                tongcv += cthd.DiemCV;
                ct.ThemCTHoaDon(cthd);

            }
            #endregion
            #region Thông tin hóa đơn
            hd.MaHD = NewHoaDon;
            hd.NgayHD = dateTimePickerNgayLapHD.Value;
            hd.SoHD = "0";
            KhachHangDTO kh = (KhachHangDTO)cbbLoaiKH.SelectedItem;
            hd.MaKH = kh.MaKH;
            hd.TenKH = txtTenKH.Text;
            hd.DiaChi = txtDiaChi.Text;
            hd.TinhTrang = false;
            hd.ThanhTien = ct.TongTien(NewHoaDon);
            NhanVienDTO nv = (NhanVienDTO)cbbMaTV.SelectedItem;
            hd.MaNV = nv.MaNV;
            hd.GhiChu = txtGhiChu.Text;
            if (IsMember(cbbLoaiKH) == 1)
                hd.LoaiKH = true;
            else
                hd.LoaiKH = false;
            bus.UpdateHoaDon(hd);
            #endregion
            BUS.SanPhamBUS sp = new BUS.SanPhamBUS();
            BUS.SoDuBUS sodu = new BUS.SoDuBUS();
            SoDuDTO sdDTO = new SoDuDTO();
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                sp.UpdateSLSP((int)dataGridView1.Rows[i].Cells[0].Value, (int)dataGridView1.Rows[i].Cells[2].Value);
                sdDTO.Ngay = dateTimePickerNgayLapHD.Value.AddMonths(1);
                sdDTO.MaSP = (int)dataGridView1.Rows[i].Cells[0].Value;
                sdDTO.SoDu = (int)dataGridView1.Rows[i].Cells[2].Value;
                sodu.ThemSoDuThangMoi(dateTimePickerNgayLapHD.Value);
                sodu.CapNhatSoDu_TungSanPham(sdDTO);
            }
            BUS.NhanVienBUS n = new BUS.NhanVienBUS();
            DTO.NhanVienDTO NV_LapHD = (NhanVienDTO)cbbMaTV.SelectedItem;
            n.UpdateNV(NV_LapHD.MaNV, tongcv);
            HoaDonGUI hdGUI = new HoaDonGUI();
            hdGUI.RefeshData();
            this.Close();
        }
        void HuyHoaDon()
        {
            BUS.HoaDonBUS bus = new BUS.HoaDonBUS();
            bus.HuyHoaDon(int.Parse(lbMaHoaDon.Text));
        }
        private void btnHuy_Click(object sender, EventArgs e)
        {
            
            this.Close();
        }
        private void cbbMaKH_SelectedIndexChanged(object sender, EventArgs e)
        {
            KhachHangDTO kh = (KhachHangDTO)cbbLoaiKH.SelectedItem;
            txtLoaiKH.Text = kh.TenKH;
            txtDiaChi.Text = kh.DiaChi;
            txtSL.Text = "";
            txtThanhTien.Text = "";
            dataGridView1.Rows.Clear();
        }
        private int ktTonTai(DataGridView dg, SanPhamDTO sp)
        {
            for (int i = 0; i < dg.RowCount; i++)
                if ((int)dg.Rows[i].Cells[0].Value==sp.MaSP)
                    return 1;
            return 0;
        }

     
    }
}
